package com.entor.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entor.dao.FinanceDaoI;
import com.entor.model.Finance;
import com.entor.model.Msg;

@Service
public class FinanceService implements BaseServiceI<Finance>{
	@Autowired
	private FinanceDaoI fdi;

	public List<Finance> find(Finance t) {
		return fdi.find(t);
	}

	public Finance get(Finance t) {
		List< Finance> list = fdi.find(t);
		return list.size()>0?list.get(0):null;
	}

	public Msg insert(Finance t) {
		Msg m = new Msg("新增失败", false, null);
		if(t.getIotype()!=null) {
			if(t.getIotype().equals("0")) {
				if(t.getAmount()!=null) {
					t.setAmount(t.getAmount()>0?t.getAmount():(0-t.getAmount()));
				}else {
					t.setAmount(0.0);
				}
			}else if (t.getIotype().equals("1")) {
				if(t.getAmount()!=null) {
					t.setAmount(t.getAmount()<0?t.getAmount():(0-t.getAmount()));
				}else {
					t.setAmount(0.0);
				}
			}else {
				return new Msg("无效类型，新增失败", false);
			}
		}
		
		int i = fdi.insert(t);
		if(i>0) {
			m = new Msg("新增成功", true, null);
		}
		return m;
	}

	public Msg batchInsert(String objs) {
		// TODO Auto-generated method stub
		return null;
	}

	public Msg edit(Finance t) {
		Msg m = new Msg("修改失败", false, null);
		if(t.getIotype()!=null) {
			if(t.getIotype().equals("0")) {
				if(t.getAmount()!=null) {
					t.setAmount(t.getAmount()>0?t.getAmount():(0-t.getAmount()));
				}else {
					t.setAmount(0.0);
				}
			}else if (t.getIotype().equals("1")) {
				if(t.getAmount()!=null) {
					t.setAmount(t.getAmount()<0?t.getAmount():(0-t.getAmount()));
				}else {
					t.setAmount(0.0);
				}
			}else {
				return new Msg("无效类型，新增失败", false);
			}
		}
		
		int i = fdi.edit(t);
		if(i>0) {
			m = new Msg("修改成功", true, null);
		}
		return m;
	}

	public Msg remove(Integer id) {
		Msg m = new Msg("删除失败", false, null);
		int i = fdi.remove(id);
		if(i>0) {
			m = new Msg("删除成功", true, null);
		}
		return m;
	}

	public Msg batchRemove(String ids) {
		String idss[] = ids.split(",");
		List<Integer> list = new ArrayList<Integer>();
		for (String str : idss) {
			list.add(Integer.parseInt(str));
		}
		Msg m = new Msg("删除失败", false, null);
		int i = fdi.batchRemove(list);
		if(i>0) {
			m = new Msg("成功删除"+i+"条记录", true, null);
		}
		return m;
		
	}
	
}
